<?xml version='1.0' encoding='iso-8859-1'?>
<!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
	<head>
		<title>
			getpib.c
			</title>
		<meta http-equiv='content-type' content='text/html;iso-8859-1'/>
		<meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
		<meta name='author' content='cmaier@cmassoc.net'/>
		<meta name='robots' content='noindex,nofollow'/>
		<link href='toolkit.css' rel='stylesheet' type='text/css'/>
		</head>
	<body>
		<div class='headerlink'>
			[<a href='getoptv.c.html' title=' getoptv.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='GetProperty.c.html' title=' GetProperty.c '>NEXT</a>]
			</div>
<pre>
/*====================================================================*
 *
 *   Copyright (c) 2013 Qualcomm Atheros, Inc.
 *
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or 
 *   without modification, are permitted (subject to the limitations 
 *   in the disclaimer below) provided that the following conditions 
 *   are met:
 *
 *   * Redistributions of source code must retain the above copyright 
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above 
 *     copyright notice, this list of conditions and the following 
 *     disclaimer in the documentation and/or other materials 
 *     provided with the distribution.
 *
 *   * Neither the name of Qualcomm Atheros nor the names of 
 *     its contributors may be used to endorse or promote products 
 *     derived from this software without specific prior written 
 *     permission.
 *
 *   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE 
 *   GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE 
 *   COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR 
 *   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
 *   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 *   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 *   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 *   OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 *
 *--------------------------------------------------------------------*/

/*====================================================================*
 *
 *   getpib.c - PIB Data Extractor
 *
 *   Contributor(s):
 *      Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
 *
 *--------------------------------------------------------------------*/

/*====================================================================*
 *   system header files;
 *--------------------------------------------------------------------*/

#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;limits.h&gt;
#include &lt;string.h&gt;
#include &lt;ctype.h&gt;

/*====================================================================*
 *   custom header files;
 *--------------------------------------------------------------------*/

#include &quot;../tools/getoptv.h&quot;
#include &quot;../tools/memory.h&quot;
#include &quot;../tools/number.h&quot;
#include &quot;../tools/error.h&quot;
#include &quot;../tools/types.h&quot;
#include &quot;../tools/flags.h&quot;
#include &quot;../tools/files.h&quot;
#include &quot;../pib/pib.h&quot;
#include &quot;../nvm/nvm.h&quot;

/*====================================================================*
 *   custom source files;
 *--------------------------------------------------------------------*/

#ifndef MAKEFILE
#include &quot;../tools/getoptv.c&quot;
#include &quot;../tools/putoptv.c&quot;
#include &quot;../tools/version.c&quot;
#include &quot;../tools/uintspec.c&quot;
#include &quot;../tools/basespec.c&quot;
#include &quot;../tools/todigit.c&quot;
#include &quot;../tools/hexout.c&quot;
#include &quot;../tools/error.c&quot;
#include &quot;../tools/checksum32.c&quot;
#include &quot;../tools/fdchecksum32.c&quot;
#endif

#ifndef MAKEFILE
#include &quot;../nvm/nvmseek2.c&quot;
#endif

/*====================================================================*
 *   constants;
 *--------------------------------------------------------------------*/

#define GETPIB_COMMA ' '
#define GETPIB_TOOBIG &quot;object '%s' exceeds extent of &quot; SIZE_T_SPEC &quot; bytes&quot;
#define GETPIB_NOSIZE &quot;object '%s' has no length&quot;

#define GETPIB_VERBOSE (1 &lt;&lt; 0)
#define GETPIB_SILENCE (1 &lt;&lt; 1)
#define GETPIB_NEWLINE (1 &lt;&lt; 2)

/*====================================================================*
 *
 *   void getmemory (byte const * memory, size_t extent, char const * object, size_t length);
 *
 *--------------------------------------------------------------------*/

static void getmemory (byte const * memory, size_t extent, char const * object, size_t length) 

{ 
	if (length &gt; extent) 
	{ 
		error (1, ECANCELED, GETPIB_TOOBIG, object, length); 
	} 
	hexout (memory, length, ':', '\0', stdout); 
	return; 
} 

/*====================================================================*
 *
 *   void getstring (byte const * memory, size_t extent, char const * object, size_t length);
 *
 *--------------------------------------------------------------------*/

static void getstring (byte const * memory, size_t extent, char const * object, size_t length) 

{ 
	char const * string = (char const *) (memory); 
	if (length &gt; extent) 
	{ 
		error (1, ECANCELED, GETPIB_TOOBIG, object, length); 
	} 
	while (isprint (* string) &amp;&amp; (length--)) 
	{ 
		putc (* string++, stdout); 
	} 
	return; 
} 

/*====================================================================*
 *
 *   void snatch (int argc, char const * argv [], byte const * memory, size_t extent, char comma);
 *
 *   extract and print the specified data objects from memory; comma
 *   delimits consecutive objects on output; 
 *
 *--------------------------------------------------------------------*/

static void snatch (int argc, char const * argv [], byte const * memory, size_t extent, char comma) 

{ 
	size_t length = 0; 
	size_t offset = 0; 
	if (! (argc) || ! (* argv)) 
	{ 
		error (1, ECANCELED, &quot;Need an offset&quot;); 
	} 
	offset = (size_t) (basespec (* argv, 16, sizeof (uint32_t))); 
	if (offset &gt; extent) 
	{ 
		error (1, ECANCELED, &quot;offset &quot; SIZE_T_SPEC &quot; exceeds extent of &quot; SIZE_T_SPEC &quot; bytes&quot;, offset, extent); 
	} 
	memory += offset; 
	extent -= offset; 
	argc--; 
	argv++; 
	while ((argc) &amp;&amp; (* argv)) 
	{ 
		char const * object = * argv; 
		argc--; 
		argv++; 
		if (! strcmp (object, &quot;byte&quot;)) 
		{ 
			uint8_t * number = (uint8_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;%u&quot;, * number); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 
		else if (! strcmp (object, &quot;word&quot;)) 
		{ 
			uint16_t * number = (uint16_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;%u&quot;, LE16TOH (* number)); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 
		else if (! strcmp (object, &quot;long&quot;)) 
		{ 
			uint32_t * number = (uint32_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;%u&quot;, LE32TOH (* number)); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 
		else if (! strcmp (object, &quot;huge&quot;)) 
		{ 
			uint64_t * number = (uint64_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;%llu&quot;, LE64TOH (* number)); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 

#if 1

		else if (! strcmp (object, &quot;xbyte&quot;)) 
		{ 
			uint8_t * number = (uint8_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;0x%02X&quot;, * number); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 
		else if (! strcmp (object, &quot;xword&quot;)) 
		{ 
			uint16_t * number = (uint16_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;0x%04X&quot;, LE16TOH (* number)); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 
		else if (! strcmp (object, &quot;xlong&quot;)) 
		{ 
			uint32_t * number = (uint32_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;0x%08X&quot;, LE32TOH (* number)); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 
		else if (! strcmp (object, &quot;xhuge&quot;)) 
		{ 
			uint64_t * number = (uint64_t *) (memory); 
			if (sizeof (* number) &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			printf (&quot;0x%016llX&quot;, LE64TOH (* number)); 
			memory += sizeof (* number); 
			extent -= sizeof (* number); 
		} 

#endif

		else if (! strcmp (object, &quot;mac&quot;)) 
		{ 
			length = ETHER_ADDR_LEN; 
			if (length &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			getmemory (memory, extent, object, length); 
			memory += length; 
			extent -= length; 
		} 
		else if (! strcmp (object, &quot;key&quot;)) 
		{ 
			length = PIB_KEY_LEN; 
			if (length &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			getmemory (memory, extent, object, length); 
			memory += length; 
			extent -= length; 
		} 
		else if (! strcmp (object, &quot;hfid&quot;)) 
		{ 
			length = PIB_HFID_LEN; 
			if (length &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			getstring (memory, extent, object, length); 
			memory += length; 
			extent -= length; 
		} 

#if 1

		else if (! strcmp (object, &quot;adminusername&quot;) || ! strcmp (object, &quot;adminpassword&quot;) || ! strcmp (object, &quot;accessusername&quot;)) 
		{ 
			length = PIB_NAME_LEN +  1; 
			if (length &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			getstring (memory, extent, object, length); 
			memory += length; 
			extent -= length; 
		} 
		else if (! strcmp (object, &quot;accesspassword&quot;)) 
		{ 
			length = PIB_HFID_LEN +  1; 
			if (length &gt; extent) 
			{ 
				error (1, ECANCELED, GETPIB_TOOBIG, object, extent); 
			} 
			getstring (memory, extent, object, length); 
			memory += length; 
			extent -= length; 
		} 
		else if (! strcmp (object, &quot;username&quot;) || ! strcmp (object, &quot;password&quot;) || ! strcmp (object, &quot;url&quot;)) 
		{ 
			length = PIB_TEXT_LEN +  1; 
			getstring (memory, extent, object, length); 
			memory += length; 
			extent -= length; 
		} 

#endif

		else if (! strcmp (object, &quot;data&quot;)) 
		{ 
			if (! * argv) 
			{ 
				error (1, EINVAL, GETPIB_NOSIZE, object); 
			} 
			length = (unsigned) (uintspec (* argv, 1, extent)); 
			hexout (memory, length, 0, 0, stdout); 
			memory += length; 
			extent -= length; 
			argc--; 
			argv++; 
		} 
		else if (! strcmp (object, &quot;text&quot;)) 
		{ 
			if (! * argv) 
			{ 
				error (1, EINVAL, GETPIB_NOSIZE, object); 
			} 
			length = (unsigned) (uintspec (* argv, 1, extent)); 
			getstring (memory, extent, object, length); 
			memory += length; 
			extent -= length; 
			argc--; 
			argv++; 
		} 
		else if (! strcmp (object, &quot;skip&quot;)) 
		{ 
			if (! * argv) 
			{ 
				error (1, EINVAL, GETPIB_NOSIZE, object); 
			} 
			length = (unsigned) (uintspec (* argv, 1, extent)); 
			memory += length; 
			extent -= length; 
			argc--; 
			argv++; 
			continue; 
		} 
		else 
		{ 
			error (1, ENOTSUP, &quot;%s&quot;, object); 
		} 
		if ((argc) &amp;&amp; (* argv)) 
		{ 
			putc (comma, stdout); 
		} 
	} 
	return; 
} 

/*====================================================================*
 *
 *   signed pibimage1 (int argc, char const * argv [], char comma);
 * 
 *   read an entire flat parameter file into memory, edit it, save 
 *   it and display it;
 *
 *   Contributor(s):
 *	Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
 *
 *--------------------------------------------------------------------*/

static signed pibimage1 (int argc, char const * argv [], char comma) 

{ 
	signed fd; 
	off_t extent; 
	byte * memory; 
	if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1) 
	{ 
		error (1, errno, FILE_CANTOPEN, * argv); 
	} 
	if ((extent = lseek (fd, 0, SEEK_END)) == - 1) 
	{ 
		error (1, errno, FILE_CANTSIZE, * argv); 
	} 
	if (lseek (fd, 0, SEEK_SET)) 
	{ 
		error (1, errno, FILE_CANTHOME, * argv); 
	} 
	if (! (memory = malloc (extent))) 
	{ 
		error (1, errno, FILE_CANTLOAD, * argv); 
	} 
	if (read (fd, memory, extent) != extent) 
	{ 
		error (1, errno, FILE_CANTREAD, * argv); 
	} 
	close (fd); 
	snatch (argc - 1, argv +  1, memory, extent, comma); 
	free (memory); 
	return (0); 
} 

/*====================================================================*
 *
 *   signed pibimage2 (int argc, char const * argv [], char comma);
 * 
 *   read an entire flat parameter file into memory, edit it, save 
 *   it and display it;
 *
 *   Contributor(s):
 *	Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
 *
 *--------------------------------------------------------------------*/

static signed pibimage2 (int argc, char const * argv [], char comma) 

{ 
	struct nvm_header2 header; 
	signed fd; 
	off_t extent; 
	byte * memory; 
	if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1) 
	{ 
		error (1, errno, FILE_CANTOPEN, * argv); 
	} 
	if (nvmseek2 (fd, * argv, &amp; header, NVM_IMAGE_PIB)) 
	{ 
		error (1, errno, &quot;Can't find PIB image in %s&quot;, * argv); 
	} 
	extent = LE32TOH (header.ImageLength); 
	if (! (memory = malloc (extent))) 
	{ 
		error (1, errno, FILE_CANTLOAD, * argv); 
	} 
	if (read (fd, memory, extent) != extent) 
	{ 
		error (1, errno, FILE_CANTREAD, * argv); 
	} 
	close (fd); 
	snatch (argc - 1, argv +  1, memory, extent, comma); 
	free (memory); 
	return (0); 
} 

/*====================================================================*
 *
 *   signed function (int argc, char const * argv [], char comma);
 *
 *   call an appropriate parameter edit function based on the file 
 *   header;
 *
 *   older parameter files are flat with their own header; newer ones
 *   are image chains where one of image contains the parameter block;
 *
 *   
 *   Contributor(s):
 *	Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
 *
 *--------------------------------------------------------------------*/

static signed function (int argc, char const * argv [], char comma) 

{ 
	uint32_t version; 
	signed status; 
	signed fd; 
	if ((fd = open (* argv, O_BINARY | O_RDWR)) == - 1) 
	{ 
		error (1, errno, FILE_CANTOPEN, * argv); 
	} 
	if (read (fd, &amp; version, sizeof (version)) != sizeof (version)) 
	{ 
		error (1, errno, FILE_CANTREAD, * argv); 
	} 
	close (fd); 
	if (LE32TOH (version) == 0x00010001) 
	{ 
		status = pibimage2 (argc, argv, comma); 
	} 
	else 
	{ 
		status = pibimage1 (argc, argv, comma); 
	} 
	return (status); 
} 

/*====================================================================*
 *
 *   int main (int argc, char const * argv []);
 *   
 *
 *--------------------------------------------------------------------*/

int main (int argc, char const * argv []) 

{ 
	static char const * optv [] = 
	{ 
		&quot;c:qvn&quot;, 
		&quot;file offset type [size]\n\n\tstandard-length types are 'byte'|'word'|'long'|'huge'|'hfid'|'mac'|'key'\n\tvariable-length types are 'data'|'text'|'skip' and need a size&quot;, 
		&quot;PIB Data Extractor&quot;, 
		&quot;c c\tobject separator is (c) [&quot; LITERAL (GETPIB_COMMA) &quot;]&quot;, 
		&quot;n\tappend newline&quot;, 
		&quot;q\tquiet mode&quot;, 
		&quot;v\tverbose mode&quot;, 
		(char const *) (0)
	}; 
	flag_t flags = (flag_t) (0); 
	char comma = GETPIB_COMMA; 
	signed c; 
	optind = 1; 
	opterr = 1; 
	while (~ (c = getoptv (argc, argv, optv))) 
	{ 
		switch (c) 
		{ 
		case 'c': 
			comma = * optarg; 
			break; 
		case 'n': 
			_setbits (flags, GETPIB_NEWLINE); 
			break; 
		case 'q': 
			_setbits (flags, GETPIB_SILENCE); 
			break; 
		case 'v': 
			_setbits (flags, GETPIB_VERBOSE); 
			break; 
		default: 
			break; 
		} 
	} 
	argc -= optind; 
	argv += optind; 
	if ((argc) &amp;&amp; (* argv)) 
	{ 
		function (argc, argv, comma); 
		if (_anyset (flags, GETPIB_NEWLINE)) 
		{ 
			putc ('\n', stdout); 
		} 
	} 
	return (0); 
} 
</pre>
		<div class='footerlink'>
			[<a href='getoptv.c.html' title=' getoptv.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='GetProperty.c.html' title=' GetProperty.c '>NEXT</a>]
			</div>
		</body>
	</html>
